//初始化页面grid
function initDagaGrid(json) {
	var queryPanel = initPageQuery(json);//初始化查询条件
	var layout = createLayout(json);
	var table = $('<table id="dataGrid_' + json.code + '"></table>');
	layout.append(table);
	//加载字段信息
	$.ajax({
	    url: host + "showAction/getModuleFieldList.html",
	    type: 'POST',
	    async: false,
	    data: {"moduleId": json.id},
	    dataType: 'json',
		success: function(data) {
			var array = data.list;
			pageParam.fields[json.code] = array;
			//显示编辑
			var opers = [];
			var operations = "";
			eval("operations=" + json.operations);
			for (var oper in operations) {
				if (operations[oper].checked != "false") {
					opers.push("<a href='javascript: void(0)' " +
						"onclick='click_operations(\"" + json.code + "\", \"" + oper + "\", {index});return false;'>" + 
						operations[oper].text + "</a>");
				}
			}
			pageParam.operations[json.code] = operations;
			
			var colums = [];
			for (var i = 0;i < array.length;i ++) {
				if (array[i].isHide != 1 || array[i].isOperation == 1) {
					var colum = {
						field : getAliasName(array[i].tableName + "_" + array[i].fieldName),
						title : array[i].name,
						width : array[i].width ? array[i].width : 60
					};
					if (array[i].isOperation == 1) {
						colum.formatter = function(value, row, index) {
							return opers.join("&nbsp;").replace(/\{index\}/g, index);
						}
					} else if (array[i].format && array[i].format.indexOf("function") > -1) {
						eval("colum.formatter=" + array[i].format);
					} else if (array[i].type == "date") {
						if (array[i].format && array[i].format.length > 10) {
							colum.formatter = function(value, row, index) {
								return value ? value.repalce("T", " ") : "";
							}
						} else {
							colum.formatter = function(value, row, index) {
								return value ? value.split("T")[0] : "";
							}
						}
					}
					colums.push(colum);
				}
			}
			createGridButton(json, queryPanel);
			
			var gridJson = {
				url : host + 'showAction/getQueryList.html?code=' + json.code,
				fit : true,
				fitColumns : true,
				border : false,
				pagination : json.pagination == 1 ? true : false,
				autoRowHeight: false,
				striped: true,
				rownumbers: true,//显示行号
				//singleSelect: true,//只能选择一行
				pageSize : json.pageSize ? json.pageSize : 20,
				pageList : [ 10, 20, 30, 40, 50 ],
				nowrap : true,
				frozenColumns: [],
				columns : [colums],
				toolbar: "#" + queryPanel[0].id
			};
			
			var clickEvent = {};
			eval("clickEvent=" + json.clicks);
			if (clickEvent) {
				var click = clickEvent.click;
				//alertObject(click);
				//text: 单击, paramNames: code, node, index, handler: function (index, data) {}
				if(clickEvent.click) {
					gridJson.onClickRow = clickEvent.click.handler;
				}
				if(clickEvent.dblClick) {
					gridJson.onDblClickRow = clickEvent.dblClick;
				}
			}
			
			pageParam.grids[json.code] = $("#dataGrid_" + json.code).datagrid(gridJson);
		}
	});
}
function getAliasName(name) {
	if (name.length > 30) {
		name = name.substring(name.length - 30);
		if (name.indexOf("_") == 0) {
			name = name.substring(1);
		}
	}
	return name;
}
function createGridButton(json, queryPanel) {
	//显示按钮
	var btns = [];
	var buttons = "";
	pageParam.buttons[json.code] = {};
	
	if (json.buttons && json.buttons.length > 2) {
		eval("buttons=" + json.buttons);
		var table = $('<div class="grid-toolbar">&nbsp;</div>');
		queryPanel.append(table);
		for (var btn in buttons) {
			if (buttons[btn].checked != "false") {
				var button = $('<a class="easyui-linkbutton" icon="' + buttons[btn].iconCls + 
					'" href="javascript:void(0)" onclick="buttonEvent(\'' + json.code + '\', \'' + btn + '\');return false;">' + 
					buttons[btn].text + '</a>');
				table.append(button);
			}
		}
		pageParam.buttons[json.code] = buttons;
	} else {
		//不加此段无法将查询条件嵌入到grid，此问题不明白
		queryPanel.append('<div style="width: 100%;clear: both;"></div>');
	}
}
//查询按钮
function serarchFun(code) {
	var array = pageParam.queryFields[code];
	var json = {};
	for (var i = 0;i < array.length;i ++) {
		var data = array[i];
		var value = null;
		if (data.type == "date" || data.format == "yyyy-MM-dd") {
			value = $("#_QUERY_" + data.tableName + "_" + data.fieldName).datebox('getValue') + "";
			//按范围查询
			if (data.isRange == 1) {
				var val = $("#_QUERY_" + data.tableName + "_" + data.fieldName + "_END_").datebox('getValue') + "";
				if (val) {
					json[data.tableName + "_" + data.fieldName + "_END_"] = val;
				}
			}
		} else {
			value = $("#_QUERY_" + data.tableName + "_" + data.fieldName).val();
			//按范围查询
			var val = $("#_QUERY_" + data.tableName + "_" + data.fieldName + "_END_").val();
			if (data.isRange == 1 && val != undefind && val != "") {
				json[data.tableName + "_" + data.fieldName + "_END_"] = val;
			}
		}
		if (value) {
			json[data.tableName + "_" + data.fieldName] = value;
		}
	}
	pageParam.grids[code].datagrid('load', json);
}
//清空条件
function cleanFun(code) {
	$('#searchForm input').val('');
	if (pageParam.grids[code]) {
		pageParam.grids[code].datagrid('load', {});
	}
}
function reloadGrid(code) {
	if (pageParam.grids[code]) {
		pageParam.grids[code].datagrid("reload");
	}
	var win = baseJs.getOpenWindow();
	if (win.pageParam.grids[code]) {
		win.pageParam.grids[code].datagrid("reload");
	}
}